package com.example.fourinarow;
//Class to handle the logic of the game.
public class GameMatrix {
	private int gameModel[][] = new int[6][7];

//Initialize an empty matrix.
	public GameMatrix() {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				this.gameModel[i][j] = 0;
			}
		}
	}
//Reset the matrix.
	public void resetGame() {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				this.gameModel[i][j] = 0;
			}
		}

	}
//Check if position is free and place pollet if so.
	public boolean getFreePositionInColumn(int y, int x, int player) {
		boolean free = false;
		int i;
		for ( i = 5; i >= 0; i--) {
			if (gameModel[i][x] == 0) {
				
				break;
			} else {
				free = false;
			}				
		}
		
		if(y == i){
			if(player == 1){
			gameModel[i][x] = 1;
			
			}else{
				gameModel[i][x] =2;
			}
			free = true;
		}
		return free;
	}
//Method to check if a player has managed to win.
	public boolean checkWinForPlayer(int player) {
		boolean win = false;
		if(checkHorizontal(player) || checkVertical(player)||checkDiagonal(player)){
			win = true;
		}else {
			win = false;
		}
		
		return win;
	}
//Method to manually set a postion in matrix.
	public void updateMatrix(int positionX, int positionY, int player) {
		gameModel[positionX][positionY] = player;
	}


	

	private boolean checkHorizontal(int player) {
		int sumOfMarkers = 0;
		boolean result = false;
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				if (gameModel[i][j] == player) {
					sumOfMarkers++;
					if (sumOfMarkers >= 4)
						result = true;
				} else
					sumOfMarkers = 0;
			}
			sumOfMarkers = 0;
		}
		return result;
	}

	private boolean checkVertical(int player) {
		int sumOfMarkers = 0;
		boolean result = false;
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 6; j++) {
				if (gameModel[j][i] == player) {
					sumOfMarkers++;
					if (sumOfMarkers >= 4)
						result = true;

				} else
					sumOfMarkers = 0;
			}
			sumOfMarkers = 0;
		}
		return result;
	}

	private boolean checkDiagonal(int player) {
		boolean result = false;
		for (int i = 0; i < 3; i++) {
			if (result == true)
				break;
			else {
				for (int j = 0; j < 4; j++) {
					if (gameModel[i][j] == player) {
						if (checkRight(i, j, player)) {
							result = true;
							break;
						}
					}
				}
			}
		}
		if (result != true) {
			for (int i = 0; i < 3; i++) {
				if (result == true)
					break;
				else {
					for (int j = 3; j < 7; j++) {
						if (gameModel[i][j] == player) {
							if (checkLeft(i, j, player)) {
								result = true;
								break;
							}
						}
					}
				}
			}
		}
		return result;
	}

	private boolean checkRight(int y, int x, int player) {
		int sumOfMarkers = 0;
		for (int i = 0; i < 4; i++) {
			if (gameModel[y + i][x + i] == player)
				sumOfMarkers++;
		}
		if (sumOfMarkers > 3)
			return true;
		else
			return false;
	}

	private boolean checkLeft(int y, int x, int player) {
		int sumOfMarkers = 0;
		for (int i = 0; i < 4; i++) {
			if (gameModel[y + i][x - i] == player)
				sumOfMarkers++;
		}
		if (sumOfMarkers > 3)
			return true;
		else
			return false;

	}
}
